﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Globalization;

namespace QuanLyPhongGiam
{
    public partial class frmSuaThongTinBiCan : Form
    {
        string MaBiCan;
        string hinhanh;
        public frmSuaThongTinBiCan(string maBiCan, string actionType)
        {
            MaBiCan = maBiCan;
            InitializeComponent();
            if (actionType.Contains(Query.detail))
            {
                txtMaBiCan.Enabled = false;
                btnSua.Visible = false;
            }
            else if (actionType.Contains(Query.insertNew))
            {
                btnSua.Text = "Thêm mới";
            }
            else if (actionType.Contains(Query.edit))
            {
                txtMaBiCan.Enabled = false;
            }
        }

        private void frmSuaThongTinSinhVien_Load(object sender, EventArgs e)
        {
            LoadCbbTruongHop();
            LoadCbbGiaiQuyet_THA_HinhThuc();
            LoadCbbTron_HinhThuc();

            if (!string.IsNullOrEmpty(MaBiCan))
            {
                LoadDataFromDBToTextBox();
            }
        }
        private void LoadCbbTruongHop()
        {
            cbbTruongHop.Items.Clear();
            cbbTruongHop.DataSource = Query.GetAllTruongHop();
            cbbTruongHop.ValueMember = "MaTruongHop";
            cbbTruongHop.DisplayMember = "TenTruongHop";
        }
        private void LoadCbbGiaiQuyet_THA_HinhThuc()
        {
            cbbGiaiQuyet_THA_HinhThuc.Items.Clear();
            cbbGiaiQuyet_THA_HinhThuc.DataSource = Query.GetAllHinhThucGiaiQuyet();
            cbbGiaiQuyet_THA_HinhThuc.ValueMember = "MaHinhThucGiaiQuyet";
            cbbGiaiQuyet_THA_HinhThuc.DisplayMember = "TenHinhThucGiaiQuyet";
        }
        private void LoadCbbTron_HinhThuc()
        {
            cbbTron_HinhThuc.Items.Clear();
            var dic = new Dictionary<int, string>();
            dic.Add(0, "--Chọn--");
            dic.Add(1, "Trốn");
            dic.Add(2, "Chết");
            cbbTron_HinhThuc.DataSource = new BindingSource(dic, null);
            cbbTron_HinhThuc.ValueMember = "Key";
            cbbTron_HinhThuc.DisplayMember = "Value";
        }

        private void LoadDataFromDBToTextBox()
        {
            BiCan bican = Query.GetBiCans(MaBiCan);
            txtMaBiCan.Text = bican.MaBiCan;
            txtTenBiCan.Text = bican.TenBiCan;
            if (bican.GioiTinh == 1)
            {
                rdNam.Checked = true;
                rdNu.Checked = false;
            }
            else
            {
                rdNam.Checked = false;
                rdNu.Checked = true;
            }
            if (bican.NgaySinh != null)
                txtNgaySinh.Text = Convert.ToDateTime(bican.NgaySinh).ToString("dd/MM/yyyy");
            if (bican.NgayThuLy != null)
                txtNgayThuLy.Text = Convert.ToDateTime(bican.NgayThuLy).ToString("dd/MM/yyyy");
            if (bican.NgayBat != null)
                txtNgayBat.Text = Convert.ToDateTime(bican.NgayBat).ToString("dd/MM/yyyy");
            txtNoiSinh.Text = bican.NoiSinh;
            txtNoiKhacDen.Text = bican.NoiKhacDen;
            txtToiDanh.Text = bican.ToiDanh;
            if (bican.TruongHop != null)
                cbbTruongHop.SelectedValue = bican.TruongHop;
            //---------------------------------Giai doan dieu tra------------------------------//
            if (bican.DT_TuNgay != null)
                txtDT_TuNgay.Text = Convert.ToDateTime(bican.DT_TuNgay).ToString("dd/MM/yyyy");
            if (bican.DT_DenNgay != null)
                txtDT_DenNgay.Text = Convert.ToDateTime(bican.DT_DenNgay).ToString("dd/MM/yyyy");

            if (bican.DT_1_TuNgay != null)
                txtDT_1_TuNgay.Text = Convert.ToDateTime(bican.DT_1_TuNgay).ToString("dd/MM/yyyy");
            if (bican.DT_1_DenNgay != null)
                txtDT_1_DenNgay.Text = Convert.ToDateTime(bican.DT_1_DenNgay).ToString("dd/MM/yyyy");

            if (bican.DT_2_TuNgay != null)
                txtDT_2_TuNgay.Text = Convert.ToDateTime(bican.DT_2_TuNgay).ToString("dd/MM/yyyy");
            if (bican.DT_2_DenNgay != null)
                txtDT_2_DenNgay.Text = Convert.ToDateTime(bican.DT_2_DenNgay).ToString("dd/MM/yyyy");

            if (bican.DT_3_TuNgay != null)
                txtDT_3_TuNgay.Text = Convert.ToDateTime(bican.DT_3_TuNgay).ToString("dd/MM/yyyy");
            if (bican.DT_3_DenNgay != null)
                txtDT_3_DenNgay.Text = Convert.ToDateTime(bican.DT_3_DenNgay).ToString("dd/MM/yyyy");

            if (bican.DT_DacBiet_TuNgay != null)
                txtDT_DacBiet_TuNgay.Text = Convert.ToDateTime(bican.DT_DacBiet_TuNgay).ToString("dd/MM/yyyy");
            if (bican.DT_DacBiet_DenNgay != null)
                txtDT_DacBiet_DenNgay.Text = Convert.ToDateTime(bican.DT_DacBiet_DenNgay).ToString("dd/MM/yyyy");

            if (bican.DT_BoSung_TuNgay != null)
                txtDT_BoSung_TuNgay.Text = Convert.ToDateTime(bican.DT_BoSung_TuNgay).ToString("dd/MM/yyyy");
            if (bican.DT_BoSung_DenNgay != null)
                txtDT_BoSung_DenNgay.Text = Convert.ToDateTime(bican.DT_BoSung_DenNgay).ToString("dd/MM/yyyy");

            //---------------------------------Giai doan truy to------------------------------//
            if (bican.TT_TuNgay != null)
                txtTT_TuNgay.Text = Convert.ToDateTime(bican.TT_TuNgay).ToString("dd/MM/yyyy");
            if (bican.TT_DenNgay != null)
                txtTT_DenNgay.Text = Convert.ToDateTime(bican.TT_DenNgay).ToString("dd/MM/yyyy");

            if (bican.TT_1_TuNgay != null)
                txtTT_1_TuNgay.Text = Convert.ToDateTime(bican.TT_1_TuNgay).ToString("dd/MM/yyyy");
            if (bican.TT_1_DenNgay != null)
                txtTT_1_DenNgay.Text = Convert.ToDateTime(bican.TT_1_DenNgay).ToString("dd/MM/yyyy");

            //---------------------------------Giai doan xet xu------------------------------//
            if (bican.XX_TuNgay != null)
                txtXX_TuNgay.Text = Convert.ToDateTime(bican.XX_TuNgay).ToString("dd/MM/yyyy");
            if (bican.XX_DenNgay != null)
                txtXX_DenNgay.Text = Convert.ToDateTime(bican.XX_DenNgay).ToString("dd/MM/yyyy");

            if (bican.XX_1_TuNgay != null)
                txtXX_1_TuNgay.Text = Convert.ToDateTime(bican.XX_1_TuNgay).ToString("dd/MM/yyyy");
            if (bican.XX_1_DenNgay != null)
                txtXX_1_DenNgay.Text = Convert.ToDateTime(bican.XX_1_DenNgay).ToString("dd/MM/yyyy");

            //---------------------------------Giai doan thi hanh an------------------------------//
            //1
            if (bican.THA_TuNgay != null)
                txtTHA_TuNgay.Text = Convert.ToDateTime(bican.THA_TuNgay).ToString("dd/MM/yyyy");
            if (bican.THA_DenNgay != null)
                txtTHA_DenNgay.Text = Convert.ToDateTime(bican.THA_DenNgay).ToString("dd/MM/yyyy");
            //2
            if (bican.GiaiQuyet_THA_HinhThuc != null)
                cbbGiaiQuyet_THA_HinhThuc.SelectedValue = bican.GiaiQuyet_THA_HinhThuc;
            if (bican.GiaiQuyet_THA_DenNgay != null)
                txtGiaiQuyet_THA_DenNgay.Text = Convert.ToDateTime(bican.GiaiQuyet_THA_DenNgay).ToString("dd/MM/yyyy");
            //3
            txtChuyenNoiKhac_Den.Text = bican.ChuyenNoiKhac_Den;
            if (bican.ChuyenNoiKhac_Ngay != null)
                txtChuyenNoiKhac_Ngay.Text = Convert.ToDateTime(bican.ChuyenNoiKhac_Ngay).ToString("dd/MM/yyyy");
            //---------------------------------Giai doan xet xu phuc tham------------------------------//
            //4
            if (bican.XXPT_TuNgay != null)
                txtXXPT_TuNgay.Text = Convert.ToDateTime(bican.XXPT_TuNgay).ToString("dd/MM/yyyy");
            if (bican.XXPT_DenNgay != null)
                txtXXPT_DenNgay.Text = Convert.ToDateTime(bican.XXPT_DenNgay).ToString("dd/MM/yyyy");
            //---------------------------------Giai doan khang cao khang nghi------------------------------//
            if (bican.KhangCao_Ngay != null)
                txtKhangCaoKhangNghi_Ngay.Text = Convert.ToDateTime(bican.KhangCao_Ngay).ToString("dd/MM/yyyy");
            if (bican.KhangCao_HinhThuc != null)
                txtKhangCao_HinhThuc.Text = bican.KhangCao_HinhThuc;

            //tron chet
            if (bican.Tron_HinhThuc != null)
            {
                if (bican.Tron_HinhThuc == 0)//tron
                    cbbTron_HinhThuc.SelectedIndex = 1;
                else if (bican.Tron_HinhThuc == 1)//chet
                    cbbTron_HinhThuc.SelectedIndex = 2;
                else cbbTron_HinhThuc.SelectedIndex = 0;// ko chon
            }
            if (bican.Tron_Ngay != null)
                txtTron_Ngay.Text = Convert.ToDateTime(bican.Tron_Ngay).ToString("dd/MM/yyyy");
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
            frmDSSV frm = new frmDSSV();
            frm.Show();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            int result = -1;
            if (!string.IsNullOrEmpty(MaBiCan))//Edit 
            {
                BiCan bicanUpdate = Query.GetBiCans(MaBiCan);
                bicanUpdate = FillDataForBiCan(bicanUpdate);
                result = Query.UpdateBiCans(bicanUpdate);
            }
            else//Insert New
            {
                using (var db = new QuanLySinhVienEntities())
                {
                    if (db.BiCans.Any(o => o.MaBiCan == txtMaBiCan.Text.Trim()))
                    {
                        MessageBox.Show("ĐÃ TỒN TẠI BỊ CAN NÀY", "THÔNG BÁO");
                        return;
                    }
                    else
                    {
                        BiCan bicanUpdate = new BiCan();
                        bicanUpdate = FillDataForBiCan(bicanUpdate);
                        result = Query.AddBiCans(bicanUpdate);
                    }
                }
            }
            if (result != -1)
                MessageBox.Show("CẬP NHẬT THÀNH CÔNG", "THÔNG BÁO");
            else
                MessageBox.Show("CÓ LỖI TRONG QUÁ TRÌNH THỰC HIỆN", "THÔNG BÁO");

            this.Close();
            frmDSSV frm = new frmDSSV();
            frm.Show();
        }

        private BiCan FillDataForBiCan(BiCan biCan)
        {
            string extraHour = " 11:00:00 am";
            CultureInfo ci = new CultureInfo("en-US");
            string formatToDB = "dd/MM/yyyy h:mm:ss tt";

            biCan.MaBiCan = txtMaBiCan.Text.Trim();
            biCan.TenBiCan = txtTenBiCan.Text;
            if (rdNam.Checked)
                biCan.GioiTinh = 1;
            else
                biCan.GioiTinh = 0;
            if (!txtNgaySinh.Text.Contains("  /  /"))
                biCan.NgaySinh = DateTime.ParseExact(txtNgaySinh.Text + extraHour, formatToDB, ci);
            else biCan.DT_TuNgay = null;
            if (!txtNgayThuLy.Text.Contains("  /  /"))
                biCan.NgayThuLy = DateTime.ParseExact(txtNgayThuLy.Text + extraHour, formatToDB, ci);
            else biCan.DT_TuNgay = null;
            if (!txtNgayBat.Text.Contains("  /  /"))
                biCan.NgayBat = DateTime.ParseExact(txtNgayBat.Text + extraHour, formatToDB, ci);
            else biCan.DT_TuNgay = null;
            biCan.NoiSinh = txtNoiSinh.Text;
            //Combobox TruongHop 
            biCan.TruongHop = Convert.ToInt32(cbbTruongHop.SelectedValue);
            biCan.NoiKhacDen = txtNoiKhacDen.Text;
            biCan.ToiDanh = txtToiDanh.Text;

            //Tam Giam
            if (!txtDT_TuNgay.Text.Contains("  /  /"))
                biCan.DT_TuNgay = DateTime.ParseExact(txtDT_TuNgay.Text + extraHour, formatToDB, ci);
            else
                biCan.DT_TuNgay = null;

            if (!txtDT_DenNgay.Text.Contains("  /  /"))
                biCan.DT_DenNgay = DateTime.ParseExact(txtDT_DenNgay.Text + extraHour, formatToDB, ci);
            else biCan.DT_DenNgay = null;

            if (!txtDT_1_TuNgay.Text.Contains("  /  /"))
                biCan.DT_1_TuNgay = DateTime.ParseExact(txtDT_1_TuNgay.Text + extraHour, formatToDB, ci);
            else biCan.DT_1_TuNgay = null;
            if (!txtDT_1_DenNgay.Text.Contains("  /  /"))
                biCan.DT_1_DenNgay = DateTime.ParseExact(txtDT_1_DenNgay.Text + extraHour, formatToDB, ci);
            else biCan.DT_1_DenNgay = null;

            if (!txtDT_2_TuNgay.Text.Contains("  /  /"))
                biCan.DT_2_TuNgay = DateTime.ParseExact(txtDT_2_TuNgay.Text + extraHour, formatToDB, ci);
            else biCan.DT_2_TuNgay = null;
            if (!txtDT_2_DenNgay.Text.Contains("  /  /"))
                biCan.DT_2_DenNgay = DateTime.ParseExact(txtDT_2_DenNgay.Text + extraHour, formatToDB, ci);
            else biCan.DT_2_DenNgay = null;

            if (!txtDT_3_TuNgay.Text.Contains("  /  /"))
                biCan.DT_3_TuNgay = DateTime.ParseExact(txtDT_3_TuNgay.Text + extraHour, formatToDB, ci);
            else biCan.DT_3_TuNgay = null;
            if (!txtDT_3_DenNgay.Text.Contains("  /  /"))
                biCan.DT_3_DenNgay = DateTime.ParseExact(txtDT_3_DenNgay.Text + extraHour, formatToDB, ci);
            else biCan.DT_3_DenNgay = null;

            if (!txtDT_DacBiet_TuNgay.Text.Contains("  /  /"))
                biCan.DT_DacBiet_TuNgay = DateTime.ParseExact(txtDT_DacBiet_TuNgay.Text + extraHour, formatToDB, ci);
            else biCan.DT_DacBiet_TuNgay = null;
            if (!txtDT_DacBiet_DenNgay.Text.Contains("  /  /"))
                biCan.DT_DacBiet_DenNgay = DateTime.ParseExact(txtDT_DacBiet_DenNgay.Text + extraHour, formatToDB, ci);
            else biCan.DT_DacBiet_DenNgay = null;

            if (!txtDT_BoSung_TuNgay.Text.Contains("  /  /"))
                biCan.DT_BoSung_TuNgay = DateTime.ParseExact(txtDT_BoSung_TuNgay.Text + extraHour, formatToDB, ci);
            else biCan.DT_BoSung_TuNgay = null;
            if (!txtDT_BoSung_DenNgay.Text.Contains("  /  /"))
                biCan.DT_BoSung_DenNgay = DateTime.ParseExact(txtDT_BoSung_DenNgay.Text + extraHour, formatToDB, ci);
            else biCan.DT_BoSung_DenNgay = null;

            //Truy to
            if (!txtTT_TuNgay.Text.Contains("  /  /"))
                biCan.TT_TuNgay = DateTime.ParseExact(txtTT_TuNgay.Text + extraHour, formatToDB, ci);
            else biCan.TT_TuNgay = null;
            if (!txtTT_DenNgay.Text.Contains("  /  /"))
                biCan.TT_DenNgay = DateTime.ParseExact(txtTT_DenNgay.Text + extraHour, formatToDB, ci);
            else biCan.TT_DenNgay = null;

            if (!txtTT_1_TuNgay.Text.Contains("  /  /"))
                biCan.TT_1_TuNgay = DateTime.ParseExact(txtTT_1_TuNgay.Text + extraHour, formatToDB, ci);
            else biCan.TT_1_TuNgay = null;
            if (!txtTT_1_DenNgay.Text.Contains("  /  /"))
                biCan.TT_1_DenNgay = DateTime.ParseExact(txtTT_1_DenNgay.Text + extraHour, formatToDB, ci);
            else biCan.TT_1_DenNgay = null;

            //Xet xu
            if (!txtXX_TuNgay.Text.Contains("  /  /"))
                biCan.XX_TuNgay = DateTime.ParseExact(txtXX_TuNgay.Text + extraHour, formatToDB, ci);
            else biCan.XX_TuNgay = null;
            if (!txtXX_DenNgay.Text.Contains("  /  /"))
                biCan.XX_DenNgay = DateTime.ParseExact(txtXX_DenNgay.Text + extraHour, formatToDB, ci);
            else biCan.XX_DenNgay = null;

            if (!txtXX_1_TuNgay.Text.Contains("  /  /"))
                biCan.XX_1_TuNgay = DateTime.ParseExact(txtXX_1_TuNgay.Text + extraHour, formatToDB, ci);
            else biCan.XX_1_TuNgay = null;
            if (!txtXX_1_DenNgay.Text.Contains("  /  /"))
                biCan.XX_1_DenNgay = DateTime.ParseExact(txtXX_1_DenNgay.Text + extraHour, formatToDB, ci);
            else biCan.XX_1_DenNgay = null;

            //Thi hanh an
            if (!txtTHA_TuNgay.Text.Contains("  /  /"))
                biCan.THA_TuNgay = DateTime.ParseExact(txtTHA_TuNgay.Text + extraHour, formatToDB, ci);
            else biCan.THA_TuNgay = null;
            if (!txtTHA_DenNgay.Text.Contains("  /  /"))
                biCan.THA_DenNgay = DateTime.ParseExact(txtTHA_DenNgay.Text + extraHour, formatToDB, ci);
            else biCan.THA_DenNgay = null;

            //Combobox GiaiQuyet_THA_HinhThuc 
            biCan.GiaiQuyet_THA_HinhThuc = Convert.ToInt32(cbbGiaiQuyet_THA_HinhThuc.SelectedValue);
            if (!txtGiaiQuyet_THA_DenNgay.Text.Contains("  /  /"))
                biCan.GiaiQuyet_THA_DenNgay = DateTime.ParseExact(txtGiaiQuyet_THA_DenNgay.Text + extraHour, formatToDB, ci);
            else biCan.GiaiQuyet_THA_DenNgay = null;

            //Chuyen Noi khac
            biCan.ChuyenNoiKhac_Den = txtChuyenNoiKhac_Den.Text;
            if (!txtChuyenNoiKhac_Ngay.Text.Contains("  /  /"))
                biCan.ChuyenNoiKhac_Ngay = DateTime.ParseExact(txtChuyenNoiKhac_Ngay.Text + extraHour, formatToDB, ci);
            else biCan.ChuyenNoiKhac_Ngay = null;

            //Xet xu phuc tham
            if (!txtXXPT_TuNgay.Text.Contains("  /  /"))
                biCan.XXPT_TuNgay = DateTime.ParseExact(txtXXPT_TuNgay.Text + extraHour, formatToDB, ci);
            else biCan.XXPT_TuNgay = null;
            if (!txtXXPT_DenNgay.Text.Contains("  /  /"))
                biCan.XXPT_DenNgay = DateTime.ParseExact(txtXXPT_DenNgay.Text + extraHour, formatToDB, ci);
            else biCan.XXPT_DenNgay = null;

            //Khang cao
            if (!txtKhangCaoKhangNghi_Ngay.Text.Contains("  /  /"))
                biCan.KhangCao_Ngay = DateTime.ParseExact(txtKhangCaoKhangNghi_Ngay.Text + extraHour, formatToDB, ci);
            else biCan.KhangCao_Ngay = null;
            biCan.KhangCao_HinhThuc = txtKhangCao_HinhThuc.Text.Trim();

            //Tron
            if (cbbTron_HinhThuc.SelectedIndex != 0)
            {
                if (cbbTron_HinhThuc.SelectedIndex == 1) biCan.Tron_HinhThuc = 0;//tron
                else biCan.Tron_HinhThuc = 1;//chet
            }
            //biCan.Tron_HinhThuc = txtTron_HinhThuc.Text.ToString();
            if (!txtTron_Ngay.Text.Contains("  /  /"))
                biCan.Tron_Ngay = DateTime.ParseExact(txtTron_Ngay.Text + extraHour, formatToDB, ci);
            else biCan.Tron_Ngay = null;
            return biCan;
        }
        private void btnHinhAnh_Click(object sender, EventArgs e)
        {
            OpenFileDialog dl = new OpenFileDialog();
            dl.InitialDirectory = Application.StartupPath + @"hinhanh/";
            if (dl.ShowDialog() == DialogResult.OK)
            {
                hinhanh = dl.FileName.Substring(dl.FileName.LastIndexOf("\\") + 1, dl.FileName.Length - dl.FileName.LastIndexOf("\\") - 1);
                pictureBox1.Image = new Bitmap(Application.StartupPath + @"\hinhanh\" + hinhanh);
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        private void btnKetQuaHocTap_Click(object sender, EventArgs e)
        {
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void groupBox20_Enter(object sender, EventArgs e)
        {

        }
    }
}
